%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Paper: "Inequality of Opportunity and Income Redistribution" 											  
% Purpose: Make Figure B1				   
% Authors: Preuss, Reyes, Somerville, Wu         
% Required Scripts: none
% Data files: performance.csv
% Computing Environment: Macbook Pro Laptop, MacOS 14.7, 16 GB of memory,
% Apple M1 , Matlab R2022a
% Run Time: 35.4 seconds
% Last Edited: 02/19/2025 		                                              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tic; clear; close all
rng(12345);                     % set seed
dir = "...";                    % set directory (performance.csv files needs to be here)
cd(dir)

% Parameters
steps = 1000;                   % set level of granularity for the x-axis
m_lo = 1;                       % lowest relative multiplier  
m_hi = 4;                       % highest relative multiplier
b_lo = 0;                       % lowest relative boost 
b_hi = 40;                      % highest relative boost    
m_step = (m_hi - m_lo)/steps;   % back out step sizes for the multiplier 
b_step = (b_hi - b_lo)/steps;   % back out step sizes for the boosts 

%% Import and format the performance data

% Import the performance data
data = readtable(dir+"/performance.csv");

% Label data
E = data.num_correct;  % Effort
Nw=size(E,1);          % Number of workers
Ns=Nw/2;               % Number of Spectators

% Effort for each possible pairing
all_comb=nchoosek(1:Nw,2); % All possible pairs   
E1=E(all_comb(:,1));
E2=E(all_comb(:,2));

%% A) Additive Boosts 
Boosts = b_lo:b_step:b_hi;
pi_hat_b=zeros(size(Boosts));
for j = 1:size(Boosts,2)

    % Relative boost or headstart
    b=Boosts(j);

    % Player 1 lucky
    Y1=(E1+b>E2 & E1>E2);
    Y1(E1+b==E2 | E1==E2 | E1+b<E2)=[]; % exclude score ties, effort ties, and cases where it is 100% clear that P2 worked harder

    % Player 2 lucky
    Y2=(E1<E2+b & E1<E2);
    Y2(E1==E2+b | E1==E2 | E1>E2+b)=[]; % exclude score ties, effort ties, and cases where it is 100% clear that P1 worked harder

    % Empirical probability
    pi_hat_b(j)=mean([Y1;Y2]);
end

%% B) Multiplicative
Ms = m_lo:m_step:m_hi;
pi_hat_m=zeros(size(Ms));
for j = 1: size(Ms,2)

    % Relative multipler
    m=Ms(j);

    % Player 1 lucky
    Y1=(E1*m>E2 & E1>E2);
    Y1(E1*m==E2 | E1==E2 | E1*m<E2)=[]; % exclude score ties, effort ties, and cases where it is 100% clear that P2 worked harder

    % Player 2 lucky
    Y2=(E1<E2*m & E1<E2);
    Y2(E1==E2*m | E1==E2 | E1>E2*m)=[]; % exclude score ties, effort ties, and cases where it is 100% clear that P1 worked harder

    % Empirical probability
    pi_hat_m(j)=mean([Y1;Y2]);
end

%% Plot
figure
hold on
plot(Boosts,pi_hat_b,'LineWidth',2,'color',[70,130,180]./255)
plot(Boosts,pi_hat_m,'--','LineWidth',2,'color',[178,34,34]./255)
ylabel("\pi")
xlabel("$b$ or 15($m-1$)",'Interpreter','latex')
legend({'Additive boost: $b$','Multiplicative boost: $m$'},'Interpreter','latex')
set(gca,'fontsize', 14);
ylim([0.5,1])
hold off
exportgraphics(gcf,'compare_pi.pdf')
toc
